package com.yfp.client.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.yfp.client.domain.ClientMachineFavorites;
import com.yfp.client.domain.OrderDetailWait;
import com.yfp.client.domain.vo.ClientMachineFavoritesVo;
import com.yfp.client.mapper.ClientMachineFavoritesMapper;
import com.yfp.client.mapper.OrderDetailWaitMapper;
import com.yfp.client.service.ClientMachineFavoritesService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description:
 *
 * @CreateDate: 2024/5/23 16:50
 * @UpdateUser:
 * @UpdateDate: 2024/5/23 16:50
 * @UpdateRemark: 暂无修改
 * @Version: 1.0
 */
@Service
public class ClientMachineFavoritesServiceImpl implements ClientMachineFavoritesService {
    @Autowired
    private ClientMachineFavoritesMapper clientMachineFavoritesMapper;
    @Resource
    private OrderDetailWaitMapper orderDetailWaitMapper;


    /**
     * 查询客户收藏设备
     * @param clientId 客户ID
     * @param latitude 纬度
     * @param longitude 经度
     * @return
     */
    @Override
    public List<ClientMachineFavoritesVo> findClientMachineFavorites(String clientId, String latitude, String longitude) {
        return clientMachineFavoritesMapper.findClientMachineFavorites(clientId, latitude, longitude);
    }


    /**
     * 补全设备最小排队信息
     * @param list
     */
    public void findClientMachineFavoritesComplement(List<ClientMachineFavoritesVo> list) {
        if (!CollUtil.isEmpty(list)) {
            for (ClientMachineFavoritesVo favoritesVo : list) {
                List<OrderDetailWait> odwList = orderDetailWaitMapper.getByMachineId(favoritesVo.getMachineId());
                odwList.stream().filter(item -> Objects.isNull(item.getOutMeal())).forEach(item -> item.setOutMeal("A"));
                Map<String, List<OrderDetailWait>> map = odwList.stream().collect(Collectors.groupingBy(OrderDetailWait::getOutMeal));
                List<OrderDetailWait> minOutMealList = map.values().stream().min(Comparator.comparing(List::size)).orElse(new ArrayList<>());
                favoritesVo.setAwaitNum(minOutMealList.size());

                // 机器代数
                Integer type = Integer.valueOf(favoritesVo.getMachineId().substring(7, 8));
                // 当双出口的机器只有一个出餐口有排队信息时，排队信息设为0
                switch (type) {
                    case 3 :
                        if (map.size() == 1) {
                            favoritesVo.setAwaitNum(0);
                        }
                        break;
                    case 4 :
                        if (map.size() == 1) {
                            favoritesVo.setAwaitNum(0);
                        }
                        break;
                    case 5 :
                        if (map.size() == 1) {
                            favoritesVo.setAwaitNum(0);
                        }
                        break;
                }
            }
        }
    }


    /**
     * 添加客户收藏
     * @param clientMachineFavorites
     */
    @Transactional
    @Override
    public void favorites(ClientMachineFavorites clientMachineFavorites) {
        ClientMachineFavorites clientMachineFavorites1 = clientMachineFavoritesMapper.findByMachineIdAndClientId(clientMachineFavorites.getMachineId(), clientMachineFavorites.getClientId());
        if (null == clientMachineFavorites1){
            clientMachineFavoritesMapper.insert(clientMachineFavorites);
        } else {
            clientMachineFavoritesMapper.delByMachineIdAndClientId(clientMachineFavorites.getMachineId(), clientMachineFavorites.getClientId());
        }
    }
}
